package loader 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import utils.MyUtils;

	
	/**
	 * ...
	 * @author SChabanov
	 */
	public class AssetLoader extends Sprite 
	{

		private var _adata:AssetData;
		
		/**
		 * 
		 */
		private var _centerContent:Boolean;
		
		private var _width:Number = 100;
		private var _height:Number = 100;
				
		private var _assetContent:DisplayObject;
				
		private var _contentSize:Point;
		
		private var _useAspectRatio:Boolean;
		private var _scaleToContent:Boolean;
			
		private var _isStartLoad:Boolean = false;
		
		/**
		 * 
		 * @param	adata
		 * @param	className
		 * @param	aspectRatio
		 */
		public function AssetLoader(adata:AssetData,w:Number=100,h:Number=100,scaleToContent:Boolean=true) 
		{
			super();
			this._width = w;
			this._height = h;
			this._scaleToContent = scaleToContent
			this._adata = adata;
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedHandler, false, 0, true);
			ExternalLoader.addEventListener(ExternalLoader.RESOURCE_LOADED, onResourceLoadedHandler);
			
		}
		
		private function onRemovedHandler(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedHandler);
			clear();
		}
		
		public function show():void
		{
			if (_assetContent || _isStartLoad) 
			{
				if (_assetContent && _assetContent.parent == null) 
				{
					addChild(_assetContent);
					changeScale();
				}
				
				return;
			}
			_isStartLoad = true;
			ExternalLoader.removeEventListener(ExternalLoader.RESOURCE_LOADED, onResourceLoadedHandler);
			ExternalLoader.loadAsset(_adata, onAssetLoadedComplete, onProgress);
		}
		
		public function clear():void
		{
			if (_assetContent && _assetContent.parent) _assetContent.parent.removeChild(_assetContent);
		}
		
		public function drawBorder():void
		{
			graphics.clear();
			graphics.lineStyle(1, 0xFF0000);
			graphics.drawRect(0, 0, _width, _height);
		}
					
				
		/**
		 * 
		 */
		override public function get height():Number 
		{
			return _height;
		}
		 
		override public function set height(value:Number):void 
		{
			_height = value;
			changeScale();
		}
		
		override public function set width(value:Number):void 
		{
			_width = value;
			changeScale();
		}
		
		override public function get width():Number 
		{
			return _width;
		}
		/**
		 * 
		 */
		
		public function get aspectRatio():Boolean 
		{
			return _useAspectRatio;
		}
		
		public function set aspectRatio(value:Boolean):void 
		{
			_useAspectRatio = value;
			changeScale()
		}
		/**
		 * 
		 */
		public function get scaleToContent():Boolean 
		{
			return _scaleToContent;
		}
		
		public function set scaleToContent(value:Boolean):void 
		{
			_scaleToContent = value;
			changeScale();
		}
		/**
		 * 
		 */
		public function get adata():AssetData 
		{
			return _adata;
		}
		
		public function set centerContent(value:Boolean):void 
		{
			_centerContent = value;
			changePosition();
		}
		
		public function get contentSize():Point 
		{
			return _contentSize;
		}
						
	
		/**
		 * 
		 *@private
		 */
		private function changeScale():void
		{
			if (_assetContent)
			{
				if (_scaleToContent)
				{
					_assetContent.width = _width;
					_assetContent.height = _height;
					
					if (_useAspectRatio)
					{
						_assetContent.scaleX < _assetContent.scaleY ? _assetContent.scaleY = _assetContent.scaleX : 
																	  _assetContent.scaleX = _assetContent.scaleY;
					}

				}else {
					_assetContent.scaleX = 1;
					_assetContent.scaleY = 1;
				}
			}
			changePosition();
		}
		
		private function changePosition():void 
		{
			if (_assetContent)
			{
				_assetContent.x = _centerContent ? _width * .5 - _assetContent.width * .5 : 0;
				_assetContent.y = _centerContent ? _height * .5 - _assetContent.height * .5 : 0;
			}
		}
		 
		private function onResourceLoadedHandler(e:Event):void 
		{
			var classExist:Boolean = ExternalLoader.classNameExist(_adata.className)
			if (classExist)
			{
				var cls:Class = ExternalLoader.getClass(adata.className);
				ExternalLoader.removeEventListener(ExternalLoader.RESOURCE_LOADED, onResourceLoadedHandler);
				_assetContent = DisplayObject(new cls());
				addChild(_assetContent);
				_contentSize = new Point(_assetContent.width, _assetContent.height);
				changeScale();
				
			}else {
			
				if (ExternalLoader.lastLoadedResource.name == _adata.className)
				{
					ExternalLoader.removeEventListener(ExternalLoader.RESOURCE_LOADED, onResourceLoadedHandler);
					_assetContent = e.currentTarget.content;
					addChild(_assetContent)
					_contentSize = new Point(_assetContent.width, _assetContent.height);
					changeScale();
				}
			}
			
		}
		
		private function onProgress(e:ProgressEvent):void 
		{
			Logger.globalLog("className " + _adata.className + " path " + _adata.path + " " + e.currentTarget.percentLoaded)
		}
		
		private function onAssetLoadedComplete(e:Event):void 
		{
			_isStartLoad = false;
			ExternalLoader.removeEventListener(ExternalLoader.RESOURCE_LOADED, onResourceLoadedHandler);
			if (ExternalLoader.classNameExist(adata.className))
			{
				var cls:Class = ExternalLoader.getClass(adata.className);
				_assetContent = DisplayObject(new cls());
				addChild(_assetContent);
				_contentSize = new Point(_assetContent.width, _assetContent.height);
			}else {
				_assetContent =  e.currentTarget.content
				addChild(_assetContent);
				_contentSize = new Point(_assetContent.width, _assetContent.height);
			}
		
			changeScale();
			
		}
			
	}

}